//
//  ceshishiyongViewController.m
//  商城
//
//  Created by runone02 on 2018/9/19.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "ceshishiyongViewController.h"
#import <XLsn0wKit.h>

@interface ceshishiyongViewController ()<UICollectionViewDelegate, UICollectionViewDataSource> {
    NSInteger nowIndex; // 点击之后的tag值
}

@property (nonatomic, strong) NSArray *arr1;
@property (nonatomic, strong) NSArray *arr2;
@property (nonatomic, strong) NSArray *arr3;
@property (nonatomic, strong) NSMutableArray *dataArr;


@property (nonatomic, assign) BOOL isOpen; // 判断时候点击分组
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ceshishiyongViewController
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(SCREEN_WIDTH, 40);
        layout.minimumLineSpacing = 10.0;
        layout.minimumInteritemSpacing = 0.0;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 50);  //组头高度
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
        _collectionView.showsVerticalScrollIndicator = YES;
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr1 = [NSArray arrayWithObjects:@"1", @"2", nil];
    self.arr2 = [NSArray arrayWithObjects:@"222", @"333", nil];
    self.arr3 = [NSArray arrayWithObjects:@"33333", @"44444", nil];
    self.arr1 = [NSArray arrayWithObjects:@"1", @"2", nil];
    self.arr2 = [NSArray arrayWithObjects:@"222", @"333", nil];
    self.arr3 = [NSArray arrayWithObjects:@"33333", @"44444", nil];
    self.arr1 = [NSArray arrayWithObjects:@"1", @"2", nil];
    self.arr2 = [NSArray arrayWithObjects:@"222", @"333", nil];
    self.arr3 = [NSArray arrayWithObjects:@"33333", @"44444", nil];
    self.dataArr = [NSMutableArray arrayWithObjects:_arr1, _arr2, _arr3, _arr1, _arr2, _arr3,_arr1, _arr2, _arr3,nil].mutableCopy;
    _isOpen = NO; // 默认没有打开分组
    nowIndex = -1; //
    [self.view addSubview:self.collectionView];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.dataArr.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == nowIndex) {
        // 判断打开的是哪个分区
        if (_isOpen) { // 如果打开分组
            return 0;
        }
        return [self.dataArr[nowIndex] count];
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
    label.backgroundColor = [UIColor redColor];
    label.text = self.dataArr[indexPath.section][indexPath.row];
    [cell.contentView addSubview:label];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor cyanColor];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = header.bounds;
        [button setTitle:[NSString stringWithFormat:@"%ld", indexPath.section] forState:UIControlStateNormal];
        [button addTarget:self action:sel_registerName("doOpen:") forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000 + indexPath.section;
        for (UIView *view in header.subviews) {
            [view removeFromSuperview];
        } // 防止复用分区头
        [header addSubview:button];
        return header;
    } else {
        return nil;
    }
}

- (void)doOpen:(UIButton *)sender {
    NSLog(@"%ld",sender.tag);
    if (nowIndex == sender.tag - 1000) {
        _isOpen = !_isOpen;
    } else {
        _isOpen = NO;
        nowIndex = sender.tag - 1000;
    }
    [_collectionView reloadData];
 
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"第%ld组,第%ld行",indexPath.section,indexPath.row);
    
    [XLsn0wShow showTopWithText:@"监测版本更新"];

    [XLsn0wVersionManager xlsn0w_updateVersionWithAppStoreID:@"1014895889" showInCurrentController:self isShowReleaseNotes:NO];

    
}


@end
